import useFetch from './core/useFetch'
import { useCachePlugin } from './core/plugins/useCachePlugin'
import { usePollingPlugin } from './core/plugins/usePollingPlugin'
import { useReadyPlugin } from './core/plugins/useReadyPlugin'
import { useRefreshDepsPlugin } from './core/plugins/useRefreshDepsPlugin'
import { useRefreshOnWindowFocus } from './core/plugins/useRefreshOnWindowFocus'

import type { Options, Service, FetchResult } from './core/types'

function useRequest<Data = any, P extends unknown[] = any[]>(
  service: Service<ResponseData<Data>, P>,
  options?: Options<ResponseData<Data>, P>
): FetchResult<ResponseData<Data>, P> {
  return request(service, options)
}

export function request<T = any, P extends unknown[] = any[]>(service: Service<T, P>, options?: Options<T, P>): FetchResult<T, P> {
  return useFetch(service, options, [useCachePlugin, usePollingPlugin, useReadyPlugin, useRefreshDepsPlugin, useRefreshOnWindowFocus])
}

export default useRequest
